package com.seaengine.util
{
	/**
	 * 日期处理工具
	 * @author 曹鹏
	 */
	public final class DateUtil
	{
		public function DateUtil()
		{
			throw new Error("Can not be instantiated!");
		}
		
		/**
		 * 获取给定时间和给定小时数的时间间隔
		 * 比如2012年3月13日12:00:00与14点的时间间隔
		 * @param	theTime 时间
		 * @param	theHour 小时
		 * @return 时间间隔
		 */
		public static function getTimeIntervalToTheHour(theTime:uint,theHour:uint):int
		{
			var theDate:Date = new Date(theTime * 1000);
			var dInterval:uint;
			if(theDate.getHours() >= theHour)
			{
				dInterval = 24 - theDate.getHours() + theHour - 1;
			}
			else
			{
				dInterval = theHour - theDate.getHours() -1;
			}
			return dInterval * 3600 +  (60 - theDate.getMinutes()) * 60 + (60 - theDate.getSeconds());
		}
		
		/**
		 * 获取时间间隔
		 * @param	curTime		当前时刻
		 * @param	prevTime	前一时刻
		 * @return	XX小时/分钟/秒前
		 */
		public static function getTimeInterval(curTime:int, prevTime:int):String {
			var dTime:int = curTime - prevTime;
			var dDay:int  = int(dTime / (24 * 3600));	
			if( dTime < 60 ) {
				return dTime + "秒前";
			}else if( dTime < 3600 ) {
				return int(dTime/60) + "分钟前";
			}else if( dTime >= 3600 && dDay == 0  ) {
				return int(dTime / 3600) + "小时前";
			}else {				
				return getTime(prevTime * 1000);
			}
			return "";
		}
		
		/**
		 * 获取制定类型的时间间隔 
		 * @param curTime 当前时间
		 * @param preTime 前一时间
		 * @param type	  类型，0-秒，1-分钟，2-小时，3-天
		 * @return 根据制定类型计算的时间间隔值
		 * 
		 */		
		public static function getTimeIntervalByType(curTime:int,preTime:int,type:int = 0):int
		{
			var result:int = 0;
			var dTime:int = curTime - preTime;
			if(dTime < 0)
				return -1;
			if(type == 0)	// 多少秒前
			{
				result = dTime;
			}
			else if(type == 1)	// 多少分钟前
			{
				result = int(dTime / 60);
			}
			else if(type == 2)	// 多少小时前
			{
				result = int(dTime / 3600);
			}
			else if(type == 3)	// 多少天前
			{
				result = int(dTime / 3600 / 24);
			}
			return result;
		}
		
		/**
		 * 获取时间表示
		 * @param	time	时间的毫秒值
		 * @return	年-月-日 时:分:秒
		 */
		public static function getTime(time:Number):String {
			var date:Date = new Date(time);
			var y:uint = date.getFullYear();
			var m:String = ("0" + (date.getMonth () + 1)).substr(-2);
			var d:String = ("0" + date.getDate ()).substr(-2);
			var h:String = ("0" + date.getHours ()).substr(-2);
			var mi:String = ("0" + date.getMinutes()).substr(-2);
			var s:String = ("0" + date.getSeconds()).substr(-2);
			return y + "-" + m + "-" + d + " " + h + ":" + mi + ":" + s;
		}
		
		/**
		 * 获取时间的字符串表示
		 * @param	n 时间秒值
		 * @return	时:分:秒
		 */
		public static function getTimeStr(n:int):String
		{
			var h:int = n / 3600;
			var m:int = (n % 3600) / 60;
			var s:int = n % 60;
			var sh:String = String(h).length == 2 ? String(h): "0" + h;
			var sm:String = String(m).length == 2 ? String(m): "0" + m;
			var ss:String = String(s).length == 2 ? String(s): "0" + s;
			return sh + ":" + sm + ":" + ss;
		}
		
		/**
		 * 获取时间的中文表示 
		 * @param n 秒
		 * @return XX时XX分XX秒
		 * 
		 */		
		public static function getTimeStr2(n:int):String
		{
			var h:int = n / 3600;
			var m:int = (n % 3600) / 60;
			var s:int = n % 60;
			var result:String = "";
			result += h > 0 ? h + "小时" : "";
			result += m > 0 ? m + "分" : "";
			result += s > 0 ? s + "秒" : "";
			return result;
		}
	}
}